/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.miscellaneous.cloudlayouter;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import twaver.Element;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.network.InteractionMode;
import twaver.network.TNetwork;
import twaver.network.background.ColorBackground;
import twaver.network.inputhandler.DefaultInputHandler;
import twaver.network.inputhandler.InputHandler;
import twaver.network.inputhandler.InteractionInputHandler;
import twaver.network.inputhandler.SelectionInputHandler;
import demo.DemoNode;
import demo.DemoPane;
import demo.MainPane;

public class CloudLayouterDemo extends DemoPane{
	private CustomCloudLayouter cloudLayouter;
	private TDataBox box;

	private TNetwork network;
	private JToggleButton btPause;
	private JComboBox filterComboBox;
	
	public CloudLayouterDemo() {
		super();
		this.addHierarchyListener(new HierarchyListener(){
			public void hierarchyChanged(HierarchyEvent e) {
				if(e.getID()==HierarchyEvent.HIERARCHY_CHANGED&&e.getChangeFlags()==HierarchyEvent.PARENT_CHANGED){
					if(getParent()!=null){
						resetDataBox(false);
						start();
					}else{
						stop();
					}
				}
			}
		});
		initDataBox();
		initNetwork();
		
		JPanel buttonPane=new JPanel();
		filterComboBox=new JComboBox();
		filterComboBox.addItem("All Demos");
		filterComboBox.addItem("Network Demos");
		filterComboBox.addItem("Chart Demos");
		filterComboBox.addItem("Other Component Demos");
		filterComboBox.addItem("Other Demos");
		btPause=new JToggleButton("||");
		JButton btRefereshImage=new JButton("Refresh");
		buttonPane.add(filterComboBox);
		buttonPane.add(btPause);
		buttonPane.add(btRefereshImage);
		buttonPane.setOpaque(true);
		buttonPane.setBackground(new Color(255,255,255,200));
		
		network.addComponentOnCanvas(buttonPane,GridBagConstraints.NORTHEAST);

		this.add(network.getCanvas(),BorderLayout.CENTER);

		filterComboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				reload();
				network.getCanvas().repaint();
			}
		});
		
		btPause.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				if(btPause.isSelected()){
					stop();
				}else{
					start();
				}
			}
		});
		
		btRefereshImage.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				resetDataBox(true);
				network.repaint();
			}
		});
		
		initCloudLayout();
		resetDataBox(false);
	}
	
	private void initNetwork() {
		network = new TNetwork(box){
			public boolean isVisible(Element element) {
				if(!(element instanceof DemoImageNode)){
					return false;
				}
				return filterComboBox.getSelectedIndex()==0||(filterComboBox.getSelectedIndex()==((DemoImageNode)element).getCategory());
			}
			protected String getCanvasToolTipText(MouseEvent e) {
				return null;
			}
		};
		
		network.setBackground(new ColorBackground(new Color(0,0,0)));
		network.addElementDoubleClickedActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Element element=box.getLastSelectedElement();
				if(element!=null){
					Element demoNode=MainPane.box.getElementByName(element.getName());
					if(demoNode!=null){
						MainPane.box.getSelectionModel().setSelection(demoNode);
					}
				}
			}
		});
		network.setToolbar(null);
		
		network.setInteractionMode(
			new InteractionMode(new InputHandler[] {
            	new DefaultInputHandler(network),
                new SelectionInputHandler(network),
                new InteractionInputHandler(network)
       }));
	}
	
	private void resetDataBox(boolean resetImage) {
		Iterator it=box.iterator();
		while(it.hasNext()){
			DemoImageNode demoImageNode=(DemoImageNode)it.next();
			if(resetImage||!demoImageNode.isDemoLoaded()){
				demoImageNode.resetImage();
			}
		}
	}

	private Font labelFont = new Font("Tahoma",Font.PLAIN,18);
	private void initDataBox() {
		box=new TDataBox();
		Iterator it=MainPane.box.iterator();
		while(it.hasNext()){
			Element demoNode=(Element)it.next();
			if(demoNode instanceof DemoNode){
				DemoImageNode node=new DemoImageNode((DemoNode) demoNode);
				node.setName(demoNode.getName());
				node.putLabelBackground(new Color(200,200,200,180));
				node.putLabelBorderColor(Color.GRAY);
				node.putLabelColor(new Color(50,50,50));
				node.putLabelFont(labelFont);
				node.putLabelVisible(false);
				node.putLabelBorder(true);
				node.putLabelPosition(TWaverConst.POSITION_CENTER);
				node.putBorderColor(Color.GREEN.darker());
				box.addElement(node);
			}
		}
	}
	
	private void initCloudLayout() {
		cloudLayouter = new CustomCloudLayouter(network);
		this.cloudLayouter.setCeaseRate(0.8);
		this.cloudLayouter.setMoveSpeed(1);
	}
	
	public void reload(){
		cloudLayouter.reload();
		if(btPause.isSelected()){
			btPause.setSelected(false);
		}
		btPause.setText("||");
	}
	
	public void start(){
		cloudLayouter.start();
		if(btPause.isSelected()){
			btPause.setSelected(false);
		}
		btPause.setText("||");
	}
	
	public void stop(){
		cloudLayouter.stop();
		if(!btPause.isSelected()){
			btPause.setSelected(true);
		}
		btPause.setText("Run");
	}
}